package model.sequent.tokens;

import model.sequent.SequentToken;

/**
 * Represents an expression with two arguments
 * 
 * @author Sjaak Smetsers (number?)
 * @author Karen Berntsen (s0815799)
 * @author Erik Stens (s0615374)
 */
public class BiOperatorExpression implements Expression {
	private Expression		leftExpression, rightExpression;
	private SequentToken	operator;
	
	public BiOperatorExpression(Expression leftExpression, Expression rightExpression, SequentToken operator) {
		this.leftExpression = leftExpression;
		this.rightExpression = rightExpression;
		this.operator = operator;
	}
	
	public Expression getLeftExpression() {
		return leftExpression;
	}
	
	public Expression getRightExpression() {
		return rightExpression;
	}
	
	public SequentToken getOperator() {
		return operator;
	}
	
	@Override
	public int hashCode() {
		return leftExpression.hashCode() ^ rightExpression.hashCode() ^ operator.hashCode();
	}
	
	/**
	 * @return true if this BiOperatorExpression is equal to the argument BiOperatorExpression. A BiOperatorExpression may be commutative (checked with the SequentToken enum for the operator).
	 */
	@Override
	public boolean equals(Object o) {
		if (o instanceof BiOperatorExpression) {
			BiOperatorExpression boe = (BiOperatorExpression) o;
			if (operator == boe.getOperator()) {
				if (leftExpression.equals(boe.leftExpression) && rightExpression.equals(boe.rightExpression)) {
					return true;
				} else if (operator.isCommutative()) {
					return leftExpression.equals(boe.rightExpression) && rightExpression.equals(boe.leftExpression);
				} else {
					return false;
				}
			} else {
				return false;
			}
		}
		return false;
	}
	
	@Override
	public BiOperatorExpression clone() {
		return new BiOperatorExpression(leftExpression.clone(), rightExpression.clone(), operator);
	}
}
